#!/bin/bash

back_func() {
    while :;do
        echo $(($(</dev/shm/foo) + 1)) > /dev/shm/foo
        sleep 3
    done
}

pid=""

subshell1_background() {
    back_func & # which will print pid
    echo "back_func:$!"
    pid=$!
}
subshell2_background() {
    #{ back_func } &
    echo "back_func:$!"
    pid=$!
}

subprocess1_background() {
    (back_func &)
    echo "back_func:$!"
    pid=$!
}
subprocess2_background() {
    (back_func) &
    echo "back_func:$!"
    pid=$!
}

subprocess2_background
while true; do
    echo $(</dev/shm/foo)
    sleep 1
done

# No use
echo "killing $pid"
kill -9 $pid
